package com.frxuexi.park2303

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.JavascriptInterface
import com.frxuexi.base.mvi.BaseViewActivity
import com.frxuexi.park2303.databinding.ActivityWebViewBinding
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebViewClient

class WebViewActivity : BaseViewActivity<ActivityWebViewBinding>() {
    var webView:WebView?=null
    override fun getBind(): ActivityWebViewBinding {
        return ActivityWebViewBinding.inflate(layoutInflater)
    }

    override fun initView() {
        webView=binding.web
        webView?.apply {
//            loadUrl("http://www.baidu.com")
            loadUrl("file:///android_asset/test.html")
            webViewClient= WebViewClient()//内部打开
            settings.javaScriptEnabled=true//允许js交互

//            js调安卓步骤1.
            addJavascriptInterface(this@WebViewActivity,"android")
        }

        binding.btnJs.setOnClickListener {
//            <!-- 安卓调用js1.-->
            webView?.loadUrl("javascript:harmonyCallJS()")
        }
    }

//    js调安卓步骤2.
    @JavascriptInterface
    fun harmonyosAgree():String{
        return "hello"
    }

    override fun onDestroy() {
        super.onDestroy()
        if(webView!=null){
            webView?.clearHistory()
            webView?.clearCache(true)
            webView?.destroy()
            webView=null
        }
    }

}